#include "picturepaintdialog.h"
#include "ui_picturepaintdialog.h"

PicturePaintDialog::PicturePaintDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::PicturePaintDialog)
{
    ui->setupUi(this);
    this->imageIndex = 0;
    this->size = 3;

    list.append(":/pics/pics/1.jpg");
    list.append(":/pics/pics/2.jpg");
    list.append(":/pics/pics/3.jpg");

    this->path = ":/pics/pics/1.jpg";


    timer = new QTimer(this);
    timer->setInterval(1000);
    timer->setTimerType(Qt::TimerType::CoarseTimer);

    // 连接信号和槽函数
    QObject::connect(timer,SIGNAL(timeout()),this,SLOT(onTimeOut()));
}

PicturePaintDialog::~PicturePaintDialog()
{
    delete ui;
}

/**
 * @brief PicturePaintDialog::on_m_btnPre_clicked
 * 上一张
 */
void PicturePaintDialog::on_m_btnPre_clicked()
{
    if(--imageIndex < 0){
        imageIndex = 2;
    }
    //    repaint();
    update();
}
/**
 * @brief PicturePaintDialog::on_m_btnNext_clicked
 * 下一张
 */
void PicturePaintDialog::on_m_btnNext_clicked()
{
    if(++imageIndex > 2){
        imageIndex = 0;
    }
    //    repaint();
    update();
}

/**
 * @brief PicturePaintDialog::paintEvent
 * @param event
 * 绘图事件处理函数
 */
void PicturePaintDialog::paintEvent(QPaintEvent *event)
{
    // 显示一张图片
    QPainter painter(this);

    // 矩形区域
    QRect rect = ui->frame->rect();

    // 坐标系不一样，需要平移
    rect.translate(ui->frame->pos());



    QImage image(list.at(imageIndex));
    painter.drawImage(rect,image);
}

/**
 * @brief PicturePaintDialog::timerEvent
 * @param event 定时器执行的时候，调用
 */
void PicturePaintDialog::timerEvent(QTimerEvent *event)
{
    this->on_m_btnNext_clicked();
    update();
}

/**
 * @brief PicturePaintDialog::on_m_start_clicked
 * 开始摇奖
 */
void PicturePaintDialog::on_m_start_clicked()
{
    timerId = this->startTimer(1000);
}

/**
 * @brief PicturePaintDialog::on_m_end_clicked
 * 结束摇奖
 */
void PicturePaintDialog::on_m_end_clicked()
{
    this->killTimer(timerId);
}

/**
 * @brief PicturePaintDialog::on_m_yjStart_clicked
 * 摇奖开始
 */
void PicturePaintDialog::on_m_yjStart_clicked()
{
    timer->start();
}

/**
 * @brief PicturePaintDialog::on_m_yjEnd_clicked
 * 摇奖结束
 */
void PicturePaintDialog::on_m_yjEnd_clicked()
{
    timer->stop();
}

/**
 * @brief PicturePaintDialog::onTimeOut
 * 摇奖定时任务到时间时，执行
 */
void PicturePaintDialog::onTimeOut()
{
    if(++imageIndex < size){

    }else{
        imageIndex = 0;
    }

    update();
}
